<?php

// $Id$

require_once(TANGRA_MAIN_DIR.'form/gform_ctrl_with_obj.class.php');

require_once('ci_type_attr_form.class.php');
require_once('ci_type_attr_form_view.class.php');

require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/ci/tcms_ci_type_attr_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/ci/tcms_ci_attr_type_dbc.class.php');


require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/settings/tcms_settings_sitemap_dbc.class.php');


class CI_Type_Attr_Form_Ctrl extends GForm_Ctrl_With_Object {
	private $ci_type_attrs_ol_map;

	protected function create_form() {
		$form = new CI_Type_Attr_Form();

		$dbc = $this->get_dbc();

		$ol_map = TCMS_CI_Attr_Type_DBC::load_types_ol_map($dbc);
		$types_field = $form->get_field('ci_type_attr');
		array_unshift($ol_map['ids'], 0);
		$ol_map['ol_map'][0] = '---';

		$types_field->set_options($ol_map['ids']);
		$this->ci_type_attrs_ol_map = $ol_map['ol_map'];

//		$form->generate_html();
		return $form;
	}


	protected function create_form_view() {
		$ret = new ci_type_attr_Form_View($this->form);

		$types_field = $ret->get_field_view('ci_type_attr');
		$types_field->set_ol_map($this->ci_type_attrs_ol_map);


		return $ret;
	}


	protected function on_good_submit() {
		$ret = true;

		if (!ereg("^[a-z]{1}[a-z0-9_]{0,254}$", $this->form->get_field_value('hid'))) {
			$this->form->set_field_error('hid', 'invalid_hid');
			$ret = false;
		} else {
			$dbc = $this->get_dbc();
			$object = $this->get_object();
			if (!$object->get_id()) {
				$ci_type = $this->get_permanent_external_param('ci_type');
			} else {
				$ci_type = $object->get_ci_type();
			}

			if (!TCMS_CI_Type_Attr_DBC::is_unique_hid($dbc, $object->get_id(), $this->form->get_field_value('hid'), $ci_type)) {
				$this->form->set_field_error('hid', 'duplicate_hid');
				$ret = false;
			}
		}

		return $ret;
	}


	protected function on_bad_submit() {

	}


	protected function create_object() {
		$object = new TCMS_CI_Type_Attr_DBC();

		return $object;
	}


	protected function transfer_obj2form() {
		$object = $this->get_object();
		$form = $this->get_form();

		$form->set_field_value('name', $object->get_name());
		$form->set_field_value('ci_type_attr', $object->get_attr_type());
		$form->set_field_value('required', $object->get_required());
		$form->set_field_value('hid', $object->get_hid());
		$form->set_field_value('multiple', $object->get_multiple());
		$form->set_field_value('params', $object->get_params());
	}


	protected function transfer_form2obj() {
		$object = $this->get_object();
		$form = $this->get_form();

		$object->set_name(trim($form->get_field_value('name')));
		$object->set_attr_type($form->get_field_value('ci_type_attr'));
		$object->set_required($form->get_field_value('required'));
		$object->set_multiple($form->get_field_value('multiple'));
		$object->set_hid($form->get_field_value('hid'));
		$object->set_params($form->get_field_value('params'));
		if (!$object->get_id()) {
			$ci_type = $this->get_permanent_external_param('ci_type');
			$object->set_ci_type($ci_type);
		}
	}
}